package com.heba.greenmulu.diary;

import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;

import com.heba.Debug;
import com.heba.config.CommonConfig;
import com.heba.greenmulu.BaseActivity;
import com.heba.greenmulu.R;
import com.heba.utils.GlideUtil;
import com.tencent.rtmp.ITXVodPlayListener;
import com.tencent.rtmp.TXLiveConstants;
import com.tencent.rtmp.TXVodPlayer;
import com.tencent.rtmp.ui.TXCloudVideoView;

/**
 * 播放网络视频
 * <p>
 * Created by jason on 2019/8/2.
 */
public class VideoPlayNetworkActivity extends BaseActivity implements ITXVodPlayListener, View.OnClickListener {

    private ImageView imageDefaultView;

    private TXCloudVideoView mCloudVideoView;

    private TXVodPlayer mVodPlayer;

    private String imageUrl;

    private String videoUrl;

//    private String url = "http://greenmulu-1253933048.cos.ap-guangzhou.myqcloud.com/transfer_e8bab415-ba7e-4e54-bb9e-ea3307330607.mp4";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_video_play);
        initTopbar();
        findViewById();
        initView();
    }

    @Override
    protected void findViewById() {
        imageDefaultView = findViewById(R.id.image_default);
        mCloudVideoView = findViewById(R.id.video_view);
    }

    @Override
    protected void initView() {
        txt_title.setText(getResources().getString(R.string.diary_video_preview));

        //创建player对象
        mVodPlayer = new TXVodPlayer(this);
        //关联player对象与界面
        mVodPlayer.setPlayerView(mCloudVideoView);

        Bundle bundle = getIntent().getExtras();
        if (null != bundle) {
            imageUrl = bundle.getString(CommonConfig.DIARY_IMAGE, "");
            videoUrl = bundle.getString(CommonConfig.DIARY_VIDEO, "");
        }

        if (!imageUrl.isEmpty()) {
            GlideUtil.load(this, imageUrl, imageDefaultView);
//            imageDefaultView.setVisibility(View.VISIBLE);
        }

        //通过url方式 启动播放
        mVodPlayer.startPlay(videoUrl);

        mCloudVideoView.setOnClickListener(this);
        mVodPlayer.setVodListener(this);
    }

    @Override
    public void onPlayEvent(TXVodPlayer txVodPlayer, int event, Bundle bundle) {
        switch (event) {
            case TXLiveConstants.PLAY_EVT_PLAY_PROGRESS:
                //播放进度
                Debug.Log(TAG, "视频播放进度");
                break;
            case TXLiveConstants.PLAY_EVT_PLAY_LOADING:
                //播放loading
                Debug.Log(TAG, "视频播放Loading");
                break;
            case TXLiveConstants.PLAY_EVT_PLAY_BEGIN:
                //播放开始
                Debug.Log(TAG, "视频播放Begin");
//                imageDefaultView.setVisibility(View.GONE);
                break;
            case TXLiveConstants.PLAY_EVT_PLAY_END:
                //播放结束
                Debug.Log(TAG, "视频播放End");
                // 播放结束后，可以直接resume()，如果调用stop和start，会导致重新播放会黑一下
//                mVodPlayer.resume();
                mVodPlayer.stopPlay(false);
                break;
            default:
                break;
        }
    }

    @Override
    public void onNetStatus(TXVodPlayer txVodPlayer, Bundle bundle) {

    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        //销毁资源
        mVodPlayer.stopPlay(true); // true 代表清除最后一帧画面
        mCloudVideoView.onDestroy();
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.video_view:
                break;
            default:
                break;
        }
    }

}
